#include "Animal.h"
#include "EggShit.h"
#include <string>
#include <vector>

#define DAY_TREN 50;
using namespace std;

bool running = true;
bool isPlaying = true;
bool isMouseUpdated = false;
bool ischange = false;// score
bool isBackground = true;
bool key[5] = { 0, 0, 0, 0, 0 };
int speed = 5;
int score = 0;
int miss = 0;
int shit = 0;
int level = 0;
Uint32 color;
Uint32 start;
Point posegg;
SDL_Event event;
char* changeBack = "Images/background.bmp";
SDL_Surface * screen;
SDL_Surface *background;
SDL_Surface * imgGa;
SDL_Surface * imgVit;
SDL_Surface * imgNgan;
SDL_Surface * imgRo;
SDL_Surface * imgEgg;
SDL_Surface * imgShit;
//list..
vector<Animal*> listAnimal;
vector<EggShit*> listEggshit;
GameObject * ro;
SDL_Surface *loadImagePNG(string filename)
{
	SDL_Surface *image = NULL;

	SDL_Surface *optimize = NULL;
	image = IMG_Load(filename.c_str());
	if (image != NULL)
	{
		optimize = SDL_DisplayFormat(image);
		SDL_FreeSurface(image);
	}
	if (optimize != NULL)
	{
		Uint32 colorkey = SDL_MapRGBA(optimize->format, 255, 255, 255, 0);
		SDL_SetColorKey(optimize, SDL_SRCCOLORKEY, colorkey);
	}
	return optimize;
}

void showMessage(int x, int y, char* nameMes, SDL_Surface* screen, int score, int sizefont)
{
	SDL_Surface * message;
	SDL_Surface * Score;
	TTF_Font *font;
	SDL_Color textcolor = { 255, 0, 0 };
	TTF_Init();
	font = TTF_OpenFont("arial.ttf", sizefont);
	message = TTF_RenderText_Solid(font, nameMes, textcolor);
	SDL_Rect mespos;
	mespos.x = x;
	mespos.y = y;
	SDL_Rect scorepos;
	scorepos.x = mespos.x + message->clip_rect.w;
	scorepos.y = mespos.y;
	char upscore[10];
	sprintf_s(upscore, "%d", score);
	Score = TTF_RenderText_Solid(font, upscore, textcolor);
	SDL_BlitSurface(message, NULL, screen, &mespos);
	SDL_BlitSurface(Score, NULL, screen, &scorepos);
	TTF_CloseFont(font);
	TTF_Quit();
}
void DrawHelp(int x, int y, char* nameMes, SDL_Surface* screen)
{
	SDL_Surface * message;
	TTF_Font *font;
	SDL_Color textcolor = { 255, 0, 0 };
	TTF_Init();
	font = TTF_OpenFont("arial.ttf", 18);
	message = TTF_RenderText_Solid(font, nameMes, textcolor);
	SDL_Rect mespos;
	mespos.x = x;
	mespos.y = y;
	SDL_BlitSurface(message, NULL, screen, &mespos);
	TTF_CloseFont(font);
	TTF_Quit();
}

void Update()
{
	Point eggpos;
	EggShit *temp;
	bool egg = true;
	for (unsigned int i = 0; i < listAnimal.size(); i++)
	{
		listAnimal[i]->Update();
		if (listAnimal[i]->isSinh())
		{
			eggpos = listAnimal[i]->getPos();
			if (rand() % 5 == 0)
			{
				egg = !egg;
			}
			if (egg)
			{
				//egg
				temp = new EggShit(imgEgg, eggpos);
				temp->setEgg();
				listEggshit.push_back(temp);
			}
			else
			{
				//shit
				temp = new EggShit(imgShit, eggpos);
				temp->setShit();
				listEggshit.push_back(temp);
			}
		}
	}
	//egg + shit
	for (unsigned int i = 0; i < listEggshit.size(); i++)
	{
		//check roi xuong dat
		if (listEggshit[i]->isDestroy())
		{
			if (listEggshit[i]->isEgg())
			{
				miss++;
				if (miss == 3)//miss 3 egg -> gameover
				{
					isPlaying = false;
				}
			}
			listEggshit.erase(listEggshit.begin() + i);
		}
		//check lot vao ro
		if (ro->checkCollision(listEggshit[i]->getBox(), ro->getBox()))
		{
			if (listEggshit[i]->isEgg())
			{
				ischange = true;
				score += 1;
			}
			else
			{
				shit++;
				if (shit == 3)//3 shit -> gameover
				{
					isPlaying = false;
				}
			}
			listEggshit.erase(listEggshit.begin() + i);
		}
		if (score != 0 && score % 20 == 0 && ischange)
		{
			level++;
			speed += 1;
			ischange = false;
		}
		listEggshit[i]->Update(speed);
	}
	if (isMouseUpdated)
	{
		Point mouse;
		mouse.x = event.motion.x - imgRo->w / 2;
		mouse.y = GROUND - imgRo->h;
		ro->SetPosition(mouse);
		isMouseUpdated = false;
	}

	ro->Update();
	if (key[0])
		ro->Move(-22, 0);
	if (key[1])
		ro->Move(22, 0);
}
void Draw(SDL_Surface* screen)
{
	//set background
	SDL_BlitSurface(background, NULL, screen, &screen->clip_rect);
	for (unsigned int i = 0; i < listAnimal.size(); i++)
	{
		listAnimal[i]->Draw(screen);
	}
	//draw egg or shit
	for (unsigned int i = 0; i < listEggshit.size(); i++)
	{
		listEggshit[i]->Draw(screen);
	}
	//draw ro
	ro->Draw(screen);
	//draw message
	char* nameScore = "SCORE:  ";
	showMessage(5, 5, nameScore, screen, score, 18);
	char* nameMiss = "Miss: ";
	showMessage(150, 5, nameMiss, screen, miss, 18);
	char* nameShit = "Shit: ";
	showMessage(250, 5, nameShit, screen, shit, 18);
	char* Level = "Level: ";
	showMessage(450, 5, Level, screen, level, 18);
}

void DrawGameOver()
{
	color = SDL_MapRGB(screen->format, 0, 255, 0);
	char* gameOver = "Game Over Please Press K New Game              ";
	showMessage(WITCH / 4, HEIGHT / 2, gameOver, screen,NULL,30);
}
void mouseFunc()
{
	isMouseUpdated = true;
}
void Clean()
{
	SDL_FreeSurface(screen);
	SDL_FreeSurface(imgGa);
	SDL_FreeSurface(imgVit);
	SDL_FreeSurface(imgNgan);
	SDL_FreeSurface(imgRo);
	SDL_FreeSurface(imgEgg);
	SDL_FreeSurface(imgShit);
	SDL_FreeSurface(background);
	while (!listAnimal.empty())
	{
		Animal * animal = listAnimal[listAnimal.size()-1];
		listAnimal.pop_back();
		delete animal;
	}
	while (!listEggshit.empty())
	{
		EggShit * egg = listEggshit[listEggshit.size() - 1];
		listEggshit.pop_back();
		delete egg;
	}
	delete ro;
}
void InitGame()
{
	Point pos;

	SDL_Init(SDL_INIT_EVERYTHING);
	screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
	color = SDL_MapRGB(screen->format, 255, 255, 255);
	background = IMG_Load(changeBack);
	imgGa = loadImagePNG("Images/ga.bmp");
	imgVit = loadImagePNG("Images/vit.bmp");
	imgNgan = loadImagePNG("Images/ngan.bmp");
	imgRo = loadImagePNG("Images/ro.bmp");
	imgEgg = loadImagePNG("Images/egg.bmp");
	imgShit = loadImagePNG("Images/shit.bmp");
	SDL_WM_SetCaption("Hung Trung", NULL);
	//set animal

	pos.x = DAY_TREN;
	pos.y = 50;
	//set cai ro
	Point roPos;
	roPos.x = 100;
	roPos.y = GROUND - imgRo->h;
	ro = new GameObject(imgRo, roPos);
	const int FPS = 30;

	//post truoc 1 con
	listAnimal.push_back(new Animal(imgGa, pos));
}
int main(int __argc, char** argv)
{
	Point pos;

	SDL_Init(SDL_INIT_EVERYTHING);
	screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
	color = SDL_MapRGB(screen->format, 255, 255, 255);
	background = IMG_Load(changeBack);
	imgGa = loadImagePNG("Images/ga.bmp");
	imgVit = loadImagePNG("Images/vit.bmp");
	imgNgan = loadImagePNG("Images/ngan.bmp");
	imgRo = loadImagePNG("Images/ro.bmp");
	imgEgg = loadImagePNG("Images/egg.bmp");
	imgShit = loadImagePNG("Images/shit.bmp");
	SDL_WM_SetCaption("Hung Trung", NULL);
	//set animal

	pos.x = DAY_TREN;
	pos.y = 50;
	//set cai ro
	Point roPos;
	roPos.x = 100;
	roPos.y = GROUND - imgRo->h;
	ro = new GameObject(imgRo, roPos);
	const int FPS = 30;

	//post truoc 1 con
	listAnimal.push_back(new Animal(imgGa, pos));
	//loop all
	while (running)
	{
		start = SDL_GetTicks();
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{

			case SDL_MOUSEMOTION:
				mouseFunc(); break;
			case SDL_QUIT:
				running = false; break;

			case SDL_KEYDOWN:
				switch (event.key.keysym.sym)
				{
				case SDLK_LEFT:
					key[0] = 1; break;
				case SDLK_RIGHT:
					key[1] = 1; break;
				case SDLK_g:
					key[2] = 1; break;
				case SDLK_v:
					key[3] = 1; break;
				case SDLK_n:
					key[4] = 1; break;
				}
				break;
			case SDL_KEYUP:
				switch (event.key.keysym.sym)
				{
				case SDLK_LEFT:
					key[0] = 0; break;
				case SDLK_RIGHT:
					key[1] = 0; break;
				case SDLK_g:
				{
							   listAnimal.push_back(new Animal(imgGa, pos));
							   key[2] = 0;
							   break;
				}
				case SDLK_v:
				{
							   listAnimal.push_back(new Animal(imgVit, pos));
							   key[3] = 0;
							   break;
				}

				case SDLK_n:
				{
							   listAnimal.push_back(new Animal(imgNgan, pos));
							   key[4] = 0;
							   break;
				}
				case SDLK_k:
				{
							   if (!isPlaying)
							   {
								   isPlaying = true;
								   Clean();
								   score = 0;
								   miss = 0;
								   shit = 0;
								   level = 0;
								   InitGame();
								   Update();
								   Draw(screen);
								   break;
							   }

				}
				}
				break;
			default:
				break;
			}
		}
		if (isPlaying)
		{
			Update();
			Draw(screen);
		}
		else
		{
			DrawGameOver();
		}
		SDL_Flip(screen);
		if (1000 / FPS > (SDL_GetTicks() - start))
		{
			SDL_Delay(1000 / FPS - (SDL_GetTicks() - start));
		}
	}
	Clean();
	SDL_Quit();
	return 0;
}